package com.osi.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.osi.bean.Course;
import com.osi.bean.Student;
import com.osi.response.R;
import com.osi.service.CourseService;
import com.osi.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author lixin
 * @date 2021/5/5 17:24
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 管理员获取学生列表
     * @param params
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam Map<String, Object> params, Model model) {

        Student student = BeanUtil.toBean(params, Student.class);

        List<Student> studentList = studentService.queryList(student);

        model.addAttribute("studentList", studentList);

        return "/admin/student/list";
    }


    /**
     * 管理员查询学生信息
     * @param studentId
     * @param model
     * @return
     */
    @GetMapping("/info/{studentId}")
    public String info(@PathVariable("studentId") Integer studentId, ModelMap model){
        Student student = studentService.getById(studentId);
        model.addAttribute("student", student);
        return "/admin/student/edit";
    }


    /**
     * 管理员修改学生信息
     * @param params
     * @param model
     * @return
     */
    @PostMapping("/edit")
    public String edit(@RequestParam Map<String, Object> params, ModelMap model){
        Student student = BeanUtil.toBean(params, Student.class);

        if(studentService.updateById(student)) {
            return "redirect:/student/list";
        }
        model.addAttribute("student", student);
        return null;
    }

    /**
     * 查询我的选课
     * @param params
     * @param model
     * @return
     */
    @GetMapping("/myCourse")
    public String myCourse(@RequestParam Map<String, Object> params, ModelMap model){

        List<Course> courseList = courseService.queryListByStudent();

        model.addAttribute("courseList", courseList);
        return "/student/course/my-course";
    }


    @GetMapping("/info")
    public String info(ModelMap model){
        Student student = studentService.getOnlineStudent();
        model.addAttribute("student", student);
        return "/student/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(@RequestParam Map<String, Object> params, ModelMap model){
        Student student = BeanUtil.toBean(params, Student.class);

        if (ObjectUtil.isNull(studentService.selectStudentByUsername(student.getUsername()))) {
            return R.ok("用户名已存在");
        }

        if(studentService.updateById(student)) {
            return R.ok("修改成功");
        }

        return R.error("修改失败");
    }


    @GetMapping("/passwordEdit")
    public String passwordEdit(ModelMap model){
        Student student = studentService.getOnlineStudent();
        model.addAttribute("student", student);
        return "/student/password-edit";
    }



    @RequestMapping("/updatePassword")
    @ResponseBody
    public R updatePassword(@RequestParam Map<String, String> params, Model model) {
        String oldPassword = params.get("oldPassword");
        String password = params.get("password");

        Student student = studentService.getOnlineStudent();
        if (student.getPassword() != oldPassword) {
            return R.error("原密码不正确");
        }

        student.setPassword(password);
        studentService.updateById(student);
        return  R.ok("密码修改成功");
    }

    @PostMapping("/delete")
    @ResponseBody
    public R delete(@RequestParam("studentId") Integer studentId, ModelMap model){

        studentService.removeById(studentId);
        return R.ok("删除成功");
    }


    @RequestMapping("/toAddPage")
    public String toAddPage(ModelMap model){

        return "/admin/student/add";
    }

    @PostMapping("/add")
    public String add(@RequestParam Map<String, Object> params, ModelMap model){

        Student student = BeanUtil.toBean(params, Student.class);
        studentService.save(student);
        return "redirect:/student/list";
    }
}
